package com.cyj.dream.security.component;

import com.cyj.dream.core.constant.security.Constants;
import com.cyj.dream.security.exception.SelfAuth2Exception;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.SneakyThrows;

/**
 * @Description: OAuth2 异常格式化
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.security.component
 * @Author: ChenYongJia
 * @CreateTime: 2021-10-19 13:30
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
public class SosAuth2ExceptionSerializer extends StdSerializer<SelfAuth2Exception> {

	public SosAuth2ExceptionSerializer() {
		super(SelfAuth2Exception.class);
	}

	@Override
	@SneakyThrows
	public void serialize(SelfAuth2Exception value, JsonGenerator gen, SerializerProvider provider) {
		gen.writeStartObject();
		gen.writeObjectField("code", value.getCode()>0?value.getCode(): Constants.oauthError);
		gen.writeStringField("msg", value.getMessage());
		gen.writeStringField("data", value.getErrorCode());
		gen.writeStringField("success","false");
		gen.writeEndObject();
	}

}
